
namespace <?=dirname($namespace)?>;
use <?=$modelNameSpace?> as <?=basename($modelNameSpace)?>Model;
use <?=$extendClass?>;
use <?=$validateNamespace?>\<?=basename($modelNameSpace)?>Validate;
use <?=$app.'\\'.$module.'\\event\\'.$event_name?>;
use app\common\utils\JsonService;
use think\facade\View;

class <?=$controller_name?> extends <?=basename($extendClass)?>{

<?php if(in_array('create',$action)){?>
    /**
    * 新增<?=$config['tableCnName']?>

    * @return mixed
    */
    public function create(){
        if($this->request->isajax()){
            $post = $this->request->post();
            $validate = new <?=basename($modelNameSpace)?>Validate;
            if(!$validate->scene('add')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return <?=$event_name?>::create<?=($tpName)?>($post);
        }
        return View::fetch();
    }
<?php } ?>

<?php if(in_array('update',$action)){?>
    /**
    * 编辑<?=$config['tableCnName']?>

    * @return mixed
    */
    public function update(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new <?=basename($modelNameSpace)?>Validate;
            if(!$validate->scene('edit')->check($post)) {
                return JsonService::error($validate->getError());
            }
            return <?=$event_name?>::update<?=($tpName)?>($post);
        }

        $<?=$config['pri_key']?> = $this->request->get('<?=$config['pri_key']?>');
        $model = $this->findModel($<?=$config['pri_key']?>);
        return View::fetch('update',[
            'model'=>$model,
        ]);
    }
<?php } ?>

<?php if(in_array('index',$action)){?>
    /**
    * <?=$config['tableCnName']?>列表

    * @return mixed
    */
    public function index(){
        $params = array_filter(request()->param());
        $dataProvider = <?=$event_name?>::<?=lcfirst($tpName)?>List($params);
        return View::fetch('index',['dataProvider'=>$dataProvider]);
    }
<?php } ?>

<?php if(in_array('view',$action)){?>
    /**
    * 查看<?=$config['tableCnName']?>

    * @return mixed
    */
    public function view(){
        $<?=$config['pri_key']?> = $this->request->get('<?=$config['pri_key']?>');
        $model = $this->findModel($<?=$config['pri_key']?>);
        return View::fetch('view',[
            'model'=>$model,
        ]);
    }
<?php }?>

<?php if(in_array('delete',$action)){?>
    /*
    * 删除<?=$config['tableCnName']?>

    */
    public function delete(){
        if($this->request->isAjax()){
            $<?=$config['pri_key']?> = $this->request->param('<?=$config['pri_key']?>');
            try{
                $model = $this->findModel($<?=$config['pri_key']?>);
                $model->delete();
            }catch (\Exception $ex){
                return JsonService::error($ex->getMessage());
            }
            return JsonService::success('删除成功');
        }
    }
<?php }?>

    /**
    * 得到模型
    *
    * @param int $id
    * @param array $where
    *
    * @return array|false|\PDOStatement|string|\think\Model
    */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return <?=basename($modelNameSpace)?>Model::findModel($id, $where);
    }

}
